function createOrder() {
    $.ajax({
        url : "",
        dataType : "json",
        success : function(data) {
            
        }
    });
}

/////////////////////////////////////////////////////////////////////////////////////

var types;

function init() {
    $.ajax({
        url : base + "/product_type/query_all_with_product",
        dataType : "json",
        success : function(data) {
        	types = data.list;
            var list_html = "<option value='0' ></option>";
            for (var i = 0; i < data.list.length; i++) {
                var type = data.list[i];
                var tmp = "<option value='" + type.typeId + "' >" + type.typeName + "</option>";
                list_html += tmp;
            }
            $("#productTypeId").html(list_html);
            
            updateProduct(0);
			newSale();
        }
    });
}

function updateProduct() {
	var value = $("#productTypeId").val();
    var list_html = "";
	for(var i = 0; i < types.length; i++) {
		var type = types[i];
		if(value == 0 || value == type.typeId) {
			for(var j = 0; j < type.products.length; j++) {
                var product = type.products[j];
                var tmp = "<option value='" + product.productId + "' >" + product.productName + "</option>";
                list_html += tmp;
			}
            
            if(value != 0) {
            	break;
            }
		}
	}
    $("#product").html(list_html);
}

function addProduct() {
	var id = $("#product").val();
	var count = $("#count").val();
	editProduct(id, count);
}

function add(id) {
	editProduct(id, 1);
}

function sub(id) {
	editProduct(id, -1);
}

function editProduct(id, count) {
    $.ajax({
        url : base + "/sale/edit_product",
        data : {"id":id, "count":count},
        dataType : "json",
        success : function(data) {
        	var list_html = "<tr><th>产品名称</th><th>单价(元)</th><th>数量</th><th>总价(元)</th></tr>";
        	for (var i = 0; i < data.list.length; i++) {
                var line = data.list[i];
                var tmp = "<tr><td>" + line.product.productName + "</td><td>" + line.product.money.amount +
                	"</td><td><a onclick='sub(" + line.product.productId +
                	")'><span class='glyphicon glyphicon-minus-sign' aria-hidden='true'></span></a>&nbsp;&nbsp;" +
                	line.quantity + "&nbsp;&nbsp;<a onclick='add(" + line.product.productId +
                	")'><span class='glyphicon glyphicon-plus-sign' aria-hidden='true'></span></a></td><td>" +
                	line.money.amount + "</td></tr>";
                list_html += tmp;
            }
            $("#orders").html(list_html);
            $("#total").html("总数量：" + data.sum + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;总价：" + data.totalAmount.amount + " 元");
        }
    });
}

function newSale() {
    $.ajax({
        url : base + "/sale/new_sale",
        dataType : "json",
        success : function(data) {
        	var list_html = "<tr><th>产品名称</th><th>单价(元)</th><th>数量</th><th>总价(元)</th></tr>";
            $("#orders").html(list_html);
            $("#total").html("");
            $("#result_block").hide();
        }
    });
}

function finishSale() {
    $.ajax({
        url : base + "/sale/finish_sale",
        dataType : "json",
        success : function(data) {
            $("#result_block").show();
            $("#orders").html("");
            $("#total").html("");
            
        	var list_html = "<tr><th>产品名称</th><th>数量</th><th>总价(元)</th></tr>";
        	for (var i = 0; i < data.list.length; i++) {
                var line = data.list[i];
                var tmp = "<tr><td>" + line.product.productName + "</td><td>" + line.quantity + "</td><td>" +
                	line.money.amount + "</td></tr>";
                list_html += tmp;
            }
            var tmp = "<tr><td></td><td>" + data.sum + "</td><td>" + data.totalAmount.amount + "</td></tr>";
            list_html += tmp;
            $("#finish_table").html(list_html);
            $("#order_messge").html("订单号：" + data.orderId +"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;下单人员：" + data.employee.name);
            $("#sale_time").html("下单时间：" + data.createTime);
            $("#result_total").html("总金额：" + data.totalAmount.amount + "元");
            $("#shouldPay").val(data.totalAmount.amount);
        }
    });
}

function pay() {
	var actuallyPay = $("#actuallyPay").val();
	$.ajax({
		url : base + "/sale/pay",
        data : {"customerPay":actuallyPay},
		dataType : "json",
		success : function(data) {
			alert(data.msg);
			location.href = base+"/order.jsp";
		}
	});
}

function actuallyPayChange() {
	$("#surplus").val($("#actuallyPay").val() - $("#shouldPay").val());
}